package com.example.speeddating.common;

import com.example.speeddating.exception.CommonException;
import com.example.speeddating.enums.ResponseCodeEnum;

public class RespGenerator<T> {
    /**
     * 调用成功
     * @param data
     * @return
     */
    public static BaseResponse returnOK(Object data){
        return new BaseResponse(ResponseCodeEnum.CODE_200.getCode(),ResponseCodeEnum.CODE_200.getMsg(),data);
    }

    /**
     * 调用成功
     * @param data
     * @param message
     * @return
     */
    public static BaseResponse returnOK(Object data,String message){
        return new BaseResponse(ResponseCodeEnum.CODE_200.getCode(),message,data);
    }

    /**
     * 调用失败
     * @param
     * @return
     */
    public static <T> BaseResponse<T> returnCommonError(CommonException e, T t) {
        BaseResponse<T> baseResponse = new BaseResponse<>();
        if(e.getCode()==null){
            baseResponse.setCode(ResponseCodeEnum.CODE_600.getCode());
        }else {
            baseResponse.setCode(e.getCode());
            baseResponse.setMessage(e.getMessage());
            baseResponse.setData(t);
        }
        return baseResponse;
    }

    /**
     * 调用失败 服务器处理失败
     * @param
     * @return
     */
    public static <T>  BaseResponse<T> returnServerError(T t){
        BaseResponse<T> baseResponse = new BaseResponse<>();
        baseResponse.setCode(ResponseCodeEnum.CODE_500.getCode());
        baseResponse.setMessage(ResponseCodeEnum.CODE_500.getMsg());
        baseResponse.setData(t);
        return baseResponse;
    }


    /**
     * 调用失败 服务器处理失败
     * @param
     * @return
     */
    public static <T>  BaseResponse<T> returnError(Object code,String msg){
        BaseResponse<T> baseResponse = new BaseResponse<>();
        if (code==null){
            baseResponse.setCode(ResponseCodeEnum.CODE_1.getCode());
        }else {
            baseResponse.setCode((Integer)code);
        }
        baseResponse.setMessage(msg);
        baseResponse.setData(null);
        return baseResponse;
    }



}
